package 程序员面试金典;

import org.junit.Test;

import java.util.*;

public class 变位词组 {

    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> lists = new LinkedList<List<String>>();
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            char[] chars = strs[i].toCharArray();
            Arrays.sort(chars);
            String s = new String(chars);
            if (!map.containsKey(s)){
                List<String> list = new LinkedList<>();
                list.add(strs[i]);
                map.put(s,list);
            }
            else
                map.get(s).add(strs[i]);
        }
        for (String s : map.keySet()) {
            lists.add(map.get(s));
        }
        return lists;
    }
    @Test
    public void test(){
        char[] chars= {'a','b','c'};
        System.out.println(chars.toString());
        String s = new String(chars);
        System.out.println(s);
    }
}
